package com.elinkway.infinitemovies.adaction;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.elinkway.infinitemovies.adaction.download.DownloadService;
import com.elinkway.infinitemovies.adaction.download.ReportADAppDownloadReceiver;
import com.elinkway.infinitemovies.bean.JumpData;
import com.elinkway.infinitemovies.bean.ServerPayload;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.ui.activity.NewMainActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.ui.activity.commonwebview.CommonWebViewActivity;
import com.elinkway.infinitemovies.utils.StringUtil;
import com.elinkway.infinitemovies.utils.Utils;

import java.util.ArrayList;


/**
 * Created by huangxiaojian on 2017/5/26.
 *
 * 广告点击行为处理类
 */

public class CommonAdActionHandle {

    private CommonAdActionHandle() {

    }

    private static final CommonAdActionHandle mInstance = new CommonAdActionHandle();

    public static CommonAdActionHandle getInstance() {
        return mInstance;
    }

    /**
     *
     * @param context 上下文
     * @param packageName 应用包名
     * @param appName app名
     * @param appIcon app图标
     * @param downloadUrl app下载地址
     * @param dpUrl app跳转
     * @param reportDownloadUrl 开始下载上报url数组
     * @param reportDownloadedUrl 下载完成上报url数组
     * @param reportInstallUrl 开始安装上报url数组
     * @param reportInstalledUrl 安装完成上报url数组
     */
    public void downloadApp(Context context, String packageName, String appName, String appIcon, String downloadUrl, String dpUrl,
                                   ArrayList reportDownloadUrl, ArrayList reportDownloadedUrl, ArrayList reportInstallUrl, ArrayList reportInstalledUrl) {
        Intent updateIntent = new Intent(context, DownloadService.class);
        updateIntent.putExtra("packageName", packageName);
        updateIntent.putExtra("appName", appName);
        updateIntent.putExtra("appIcon", appIcon);
        updateIntent.putExtra("url", downloadUrl);
        updateIntent.putExtra("dpUrl", dpUrl);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.DOWNLOAD_URLS, reportDownloadUrl);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.DOWNLOADED_URLS, reportDownloadedUrl);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.START_INSTALL_URLS, reportInstallUrl);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.INSTALL_URLS, reportInstalledUrl);
        context.startService(updateIntent);
    }

    public void downloadApp(Context context, String packageName, String appName, String appIcon, String downloadUrl, String dpUrl,
                                   ArrayList reportDownloadUrl, ArrayList reportDownloadedUrl, ArrayList reportInstalledUrl) {
        downloadApp(context, packageName, appName, appIcon, downloadUrl, dpUrl, reportDownloadUrl, reportDownloadedUrl, null ,reportInstalledUrl);
    }

    public void downloadApp(Context context, String downloadUrl) {
        downloadApp(context, "", "", "", downloadUrl, "", null, null, null ,null);
    }

    public void downloadApp(Context context, String downloadUrl, String adPos, String appName, String packageName, boolean isInnerWebviewDownload) {
        LogUtils.d("inner downloadApp:" + downloadUrl + " adPos:" + adPos);
        Intent updateIntent = new Intent(context, DownloadService.class);
        updateIntent.putExtra("url", downloadUrl);
        updateIntent.putExtra(Utils.IS_INNER_WEBVIEW_DOWNLOAD, isInnerWebviewDownload);
        updateIntent.putExtra("packageName", packageName);
        updateIntent.putExtra("appName", appName == null ? "" : appName);
        updateIntent.putExtra(Utils.AD_POS, adPos);
        updateIntent.putExtra("appIcon", "");
        updateIntent.putExtra("dpUrl", "");
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.DOWNLOAD_URLS, null);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.DOWNLOADED_URLS, null);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.START_INSTALL_URLS, null);
        updateIntent.putStringArrayListExtra(ReportADAppDownloadReceiver.INSTALL_URLS, null);
        context.startService(updateIntent);
    }

    public void go2WebView(Context context, ServerPayload serverPayload, String adPos) {
        if (context == null || serverPayload == null) {
            return;
        }
        String url = serverPayload.lp;
        if (TextUtils.isEmpty(url)) {
            return;
        }
        com.apkfuns.logutils.LogUtils.d("inner go2WebView:" + url);
        Intent intent = new Intent(context, CommonWebViewActivity.class);
        Bundle params = new Bundle();
        params.putSerializable(Utils.AD_INFO, serverPayload);
        params.putString(Utils.AD_POS, adPos);
        intent.putExtras(params);
        context.startActivity(intent);
    }

    public void doServerAd(Context context, ServerPayload serverPayload, String adPos) {
        if (serverPayload != null && !TextUtils.isEmpty(adPos)) {
            if (serverPayload.action == ServerPayload.ACTION_H5) {
                go2WebView(context, serverPayload, adPos);
            } else if (serverPayload.action == ServerPayload.ACTION_DOWNLOAD) {
                CommonAdActionHandle.getInstance().downloadApp(context, serverPayload.lp, adPos, serverPayload.appName, serverPayload.packageName, true);
            }
        }
    }

    /**
     *
     * @param context 上下文
     * @param jumpUrl 跳转地址
     */
    public void go2WebView(Context context, String jumpUrl) {
        if (context == null || StringUtil.isEmpty(jumpUrl)) {
            return;
        }
        Intent intent = new Intent(context, CommonWebViewActivity.class);
        JumpData jumpData = new JumpData();
        jumpData.setPlayUrl(jumpUrl);
        Bundle params = new Bundle();
        params.putSerializable(Utils.CHANNEL_DTAT, jumpData);
        String fromPage = null;
        if (context instanceof NewMainActivity) {
            fromPage = NewMainActivity.class.getName();
        } else if (context instanceof VideoDetailActivity) {
            fromPage = VideoDetailActivity.class.getName();
        }
        params.putString(Utils.FROM, fromPage);
        intent.putExtras(params);
        context.startActivity(intent);
    }

    /**
     * 支持deeplink的跳转
     * 调用时得先判断支持的app是否已经安装
     *
     * @param context 上下文
     * @param dpUrl 跳转地址
     */
    public void go2AppByDpLink(Context context, String dpUrl) {
        if (context == null || StringUtil
                .isEmpty(dpUrl)) {
            return;
        }
        Intent tIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(dpUrl));
        tIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(tIntent);
    }
}
